package SwingModel;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import JDBC.CmsDatabaseConnection;

public abstract class CmsUser {

	final private String username;
	private String password;
	final private String firstName;
	final private String lastName;
	private List<CmsCourse> listOfCourse;
	private int selectedCourseIndex;
	private String replyContent;
	

	/**
	 * This constructor instantiate a user.
	 * 
	 * The getCourse value draw request on the system to fetch course list
	 * "true" == fetch the course list
	 * "false" == dont fetch course list ~ empty course list.
	 * 
	 * @param username
	 * @param firstName
	 * @param lastName
	 * @param getCourse
	 */
	public CmsUser(String username,String firstName, String lastName, boolean getCourse){
		this.username = username;
		this.firstName = firstName;
		this.lastName = lastName;
		if(getCourse) this.fetchCourseList(username);
		else listOfCourse = new ArrayList(); //empty list
	}
	
	@Override
	public String toString(){

		return "Username = " + username
				+ "\nUser Full Name = " + firstName + " " + lastName + "\n";
	}
	
	//---------------------------------------------------------
	//METHOD: fetchCourseList
	//---------------------------------------------------------
	/**
	 * This method fetch a list of courses that the user enrolled
	 * in.
	 * This method should be called when we instantiate the main user
	 * @param username
	 * @return
	 */
	public void fetchCourseList(String username){
		listOfCourse = new ArrayList<CmsCourse>();
		try{
			//Database Connection and Retrieve data
			CmsDatabaseConnection dCon = new CmsDatabaseConnection();
			PreparedStatement pstmt = dCon.getPreparedStatement();
			pstmt = dCon.connection.prepareStatement("SELECT U.courseid, C.coursetitle FROM User_Courses U, Courses C WHERE U.username = ? AND U.courseid = C.courseid GROUP BY U.courseid");
			pstmt.setString(1,username);
			ResultSet resultSet = pstmt.executeQuery();
			
			//Get List of Course
			
			while(resultSet.next()){
				CmsCourse course = new CmsCourse(resultSet.getString("courseID"), resultSet.getString("courseTitle"));
				listOfCourse.add(course);
				System.out.println(course.getCourseID() + " --- " + course.getCourseTitle());
			}
		} catch (Exception e){}
	}

	//---------------------------------------------------------
	//ANNOUNCEMENT:
	//---------------------------------------------------------
	
	
	//---------------------------------------------------------
	//MESSAGE:
	//---------------------------------------------------------

	//---------------------------------------------------------
	//PEOPLE:
	//---------------------------------------------------------
	
	//---------------------------------------------------------
	//GRADEBOOK:
	//---------------------------------------------------------
	
	//---------------------------------------------------------
	//QUIZ:
	//---------------------------------------------------------
	
	//---------------------------------------------------------
	//ASSIGNMENT:
	//---------------------------------------------------------
	
	//---------------------------------------------------------
	//LECTURE MATERIAL:
	//---------------------------------------------------------
	
	//---------------------------------------------------------
	//GETTERS AND SETTERS:
	//---------------------------------------------------------
	public String getUsername() {
		return username;
	}
	
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getFirstName() {
		return firstName;
	}
	
	public String getLastName() {
		return lastName;
	}	
	
	public List<CmsCourse> getListOfCourse() {
		return listOfCourse;
	}
	

	public int getSelectedCourseIndex() {
		return selectedCourseIndex;
	}


	public void setSelectedCourseIndex(int selectedCourseIndex) {
		this.selectedCourseIndex = selectedCourseIndex;
	}
	
	public CmsCourse getSelectedCourse(){
		return listOfCourse.get(selectedCourseIndex);
	}
	
	public String getReplyMessageContent(){
		return replyContent;
	}
	
	public void setReplyMessageContent(String replyMessageContent) {
		replyContent = replyMessageContent;	
	}
}
